package com.hhh.tools.ui.screens

import android.content.Intent
import android.content.res.Configuration
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.hhh.tools.R
import com.hhh.tools.TestActivity
import com.hhh.tools.ui.components.AppTitle
import com.hhh.tools.ui.theme.ComposeDemoTheme
import com.hhh.tools.ui.theme.line

@Preview(name = "Light Mode")
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true, name = "Dark Mode")
@Composable
fun PreviewAboutPage() {
    val navController = rememberNavController()
    AboutPage(navController)
}

@Composable
fun AboutPage(navController: NavController) {
    ComposeDemoTheme {
        Column(
            Modifier
                .background(MaterialTheme.colors.background)
                .fillMaxSize()
        ) {
            AppTitle("关于聚刷刷") {
                navController.popBackStack()
            }
            PageContent(navController)
        }
    }
}

@Composable
fun PageContent(navController: NavController) {
    val context = LocalContext.current
    Card(
        backgroundColor = Color.White,
        shape = RoundedCornerShape(4.dp),
        modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp)
    ) {
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Spacer(modifier = Modifier.height(24.dp))
            Image(
                painter = painterResource(id = R.mipmap.default_avatar4),
                contentDescription = "Compose Demo",
                modifier = Modifier
                    .size(50.dp)
            )
            Text(
                text = "聚刷刷",
                color = Color.Black,
                fontSize = 14.sp,
                modifier = Modifier.padding(10.dp)
            )
            Text(
                text = "v1.0.0",
                color = Color.Black,
                fontSize = 14.sp
            )
            Spacer(modifier = Modifier.height(10.dp))
            AboutItem("隐私政策", Modifier.clickable {
                Toast
                    .makeText(context, "隐私政策click", Toast.LENGTH_SHORT)
                    .show()
                navController.navigate("setting_page")
            })
            AboutItem("服务条款", Modifier.clickable {
                Toast
                    .makeText(context, "服务条款click", Toast.LENGTH_SHORT)
                    .show()
                val intent = Intent(context, TestActivity::class.java)
                intent.putExtra("data", "xnyr+wfjp+vutshv")
                context.startActivity(intent)
            })
        }
    }
}

@Composable
private fun AboutItem(text: String, modifier: Modifier = Modifier) {
    Box(modifier) {
        Row(
            modifier = Modifier
                .padding(horizontal = 15.dp)
                .height(50.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = text,
                color = Color.Black,
                fontSize = 14.sp
            )
            Spacer(Modifier.weight(1f))
            Icon(
                painter = painterResource(id = R.drawable.ic_right_c8c8c8_18dp),
                contentDescription = text,
                tint = Color(0xFFC8C8C8),
                modifier = Modifier
                    .size(16.dp)
            )
        }
    }
    Divider(
        modifier = Modifier
            .fillMaxWidth()
            .height(0.5.dp), color = MaterialTheme.colors.line
    )
}